import { Body, Get, Ip, Post, Req, UseGuards } from '@nestjs/common';
import { Controller } from '@nestjs/common';
import { UserService } from './user.service';
import { Public } from '../metadatas/public.metadata';

@Controller('/api/user')
export class UserController {
  constructor(private UserService: UserService) {}

  @Get('/userinfo')
  async userinfo(@Req() request: any) {
    // const { account_id } = request.user;
    // const result = await this.UserService.userinfo(account_id);
    return Promise.resolve(request.user);
  }

  @Public()
  @Post('/login')
  async login(@Body() body: any, @Ip() ip: string) {
    const result = await this.UserService.login({ ...body, ip });
    return Promise.resolve(result);
  }

  @Public()
  @Post('/register')
  async register(@Body() body: any) {
    const result = await this.UserService.register(body);
    return Promise.resolve(result);
  }

  @Public()
  @Post('/logout')
  async logout(@Req() request: any) {
    const { user } = request;
    if (!user) return Promise.resolve();
    const { account_id } = user;
    await this.UserService.logout(account_id);
    return Promise.resolve();
  }
}
